package org.example.lease.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

import org.example.lease.model.entities.UserInfo;
import org.example.lease.model.enums.BaseStatus;
import org.example.lease.model.result.Result;
import org.example.lease.service.UserInfoService;
import org.example.lease.vo.user.UserInfoQueryVo;
import org.springframework.web.bind.annotation.*;

/**
 * @author 宋孟林
 * @description
 * @date: 2025/5/24 19:59
 */
@Tag(name = "用户信息管理")
@Slf4j
@RestController
@RequestMapping("/admin/user")
public class UserInfoController {
    @Resource
    private UserInfoService userInfoService;

    @Operation(summary = "分页查询用户信息")
    @GetMapping("/page")
    public Result<IPage<UserInfo>> pageUserInfo(@RequestParam Long current,
                                                @RequestParam Long size,
                                                UserInfoQueryVo queryVo) {
        IPage<UserInfo> infoIPage = new Page<>(current, size);
        IPage<UserInfo> userInfoIPage = userInfoService.pageUserInfo(infoIPage, queryVo);
        return Result.ok(userInfoIPage);
    }

    @GetMapping("/getById")
    public Result<?> getUserById(String phone) {
        //显式指定查询哪些参数
        UserInfo userInfo = userInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class)
                .eq(UserInfo::getPhone, phone)
                .select(UserInfo::getPhone, UserInfo::getNickname, UserInfo::getPassword));
        return Result.ok(userInfo);
    }

    @Operation(summary = "根据用户id更新账号状态")
    @PostMapping("/updateStatusById")
    public Result<?> updateUserStatusById(@RequestParam Long id, @RequestParam BaseStatus status) {
        userInfoService.update(Wrappers.lambdaUpdate(UserInfo.class)
                .eq(UserInfo::getId, id).set(UserInfo::getStatus, status));
        return Result.ok();
    }
}
